import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
import 'package:wechat_manager/getx/controller_helper.dart';
import 'package:wechat_manager/network/model/content/article_model.dart';
import 'package:wechat_manager/pages/content/draft/draft_controller.dart';
import 'package:wechat_manager/pages/content/draft/draft_state.dart';

class DraftView extends StatelessWidget {
  DraftView({super.key});

 final DraftController draftController = ControllerHelper.put(DraftController());
 final DraftState state = ControllerHelper.find<DraftController>().state;

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return Scaffold(
        appBar: AppBar(title: Text("草稿箱")),
        body: ListView.separated(
          itemBuilder: (context, index) {
            return buildItem(index, state.articleItems[index]);
          },
          separatorBuilder: (context, index) {
            return Divider(height: 10, color: Colors.grey);
          },
          itemCount: state.articleItems.length,
        ),
      );
    });
  }

  Widget buildItem(int index, ArticleItemModel model) {
    List<ArticleItemContentItemModel> contentItemModels =
        model.content?.newsItem ?? [];
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 12),
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 12),
            child: Row(children: [Text(model.mediaId ?? '')]),
          ),
          ListView.separated(
            physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            itemBuilder: (context, index) {
              return buildContentItemView(index, contentItemModels[index]);
            },
            separatorBuilder: (context, index) {
              return Divider(height: 0.5, color: Colors.grey);
            },
            itemCount: contentItemModels.length,
          ),
        ],
      ),
    );
  }

  Widget buildContentItemView(int index, ArticleItemContentItemModel model) {
    return Row(
      children: [
        CachedNetworkImage(
          imageUrl: model.thumbUrl ?? "",
          width: 80,
          height: 60,
          fit: BoxFit.cover,
          errorWidget: (context, url, error) => Icon(Icons.error),
        ),
        SizedBox(width: 10,),
        Column(
          children: [
            Text("${model.title}"),
            Text("${model.author}")
          ],
        )
      ],
    );
  }
}
